package com.army.stock.web;

import com.army.stock.domain.ArmyDepartment;
import com.army.stock.service.ArmyDepartmentService;
import com.army.stock.util.CommonRegExp;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.web.url.BaseUrl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangyong on 15/1/5.
 */
@Controller
public class ArmyDepartmentController {

    private final static Logger logger = LoggerFactory.getLogger(ArmyDepartmentController.class);

    private static String NEW_PAGE = "department/new";

    private static String UPDATE_PAGE = "department/edit";

    private static String DEPARTMENTS_PAGE = "department/departments";

    @Autowired
    private ArmyDepartmentService armyDepartmentService;

    @RequestMapping(value = "/departments", method = RequestMethod.GET)
    public String Departments(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmyDepartment> pageInfo = armyDepartmentService.getArmyDepartmentPageInfo(
                baseUrl.getCurrentPage(), 20, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return DEPARTMENTS_PAGE;
    }

    @RequestMapping(value = "/departments/new", method = RequestMethod.GET)
    public String addDepartmentPage(HttpServletRequest request, ModelMap modelMap) {
        return NEW_PAGE;
    }

    @RequestMapping(value = "/departments/add", method = RequestMethod.POST)
    public String addDepartment(HttpServletRequest request, ModelMap modelMap, ArmyDepartment armyDepartment) {
        if (StringUtils.isBlank(armyDepartment.getDepartmentCode())) {
            modelMap.put("errorMsg", "部门编码不能为空");
            return NEW_PAGE;
        }
        if (!armyDepartment.getDepartmentCode().matches(CommonRegExp.CHARACTER_NUMBER)) {
            modelMap.put("errorMsg", "部门编码只能为英文中文");
            return NEW_PAGE;
        }
        if (StringUtils.isBlank(armyDepartment.getDepartmentName())) {
            modelMap.put("errorMsg", "部门名称不能为空");
            return NEW_PAGE;
        }
        ArmyDepartment temp = armyDepartmentService.getArmyDepartmentByParam(armyDepartment);
        if (temp != null) {
            modelMap.put("errorMsg", "部门名称/编码已经存在，请选择新的名称或者编码");
            return NEW_PAGE;
        }
        armyDepartmentService.addArmyDepartment(armyDepartment);
        return "redirect:/departments";
    }

    @RequestMapping(value = "/departments/{departmentId}/update", method = RequestMethod.GET)
    public String editPage(@PathVariable(value = "departmentId") String departmentId, HttpServletRequest request, ModelMap modelMap) {
        logger.info("转向修改类型页面departmentId:" + departmentId);
        modelMap.put("departmentId", departmentId);
        ArmyDepartment armydepartment = armyDepartmentService.getArmyDepartmentById((Long.valueOf(departmentId)));
        modelMap.put("armyDepartment", armydepartment);
        return UPDATE_PAGE;
    }

    @RequestMapping(value = "/departments/update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, ModelMap modelMap, ArmyDepartment armyDepartment) {
        if (StringUtils.isBlank(armyDepartment.getDepartmentCode())) {
            modelMap.put("errorMsg", "部门编码不能为空");
            return NEW_PAGE;
        }
        if (StringUtils.isBlank(armyDepartment.getDepartmentName())) {
            modelMap.put("errorMsg", "部门编码名称不能为空");
            return NEW_PAGE;
        }
        armyDepartmentService.updateArmyDepartment(armyDepartment);
        return "redirect:/departments";
    }

    @RequestMapping(value = "/departments/del", method = RequestMethod.POST)
    @ResponseBody
    public String del(HttpServletRequest request, ModelMap modelMap) {
        String id = request.getParameter("id");
        logger.info("尝试删除部门编号:" + id);
        Integer affectedRows = armyDepartmentService.delArmyDepartment(Long.valueOf(id));
        return String.valueOf(affectedRows);
    }


}
